# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import re
from openerp.osv import fields, osv
from openerp.tools.translate import _

class product_product(osv.osv):
	_inherit = "product.product"
	
	#增加part_no_external的显示
	def name_get(self, cr, user, ids, context=None):
		if context is None:
			context = {}
		if isinstance(ids, (int, long)):
			ids = [ids]
		if not len(ids):
			return []
		def _name_get(d):
			name = d.get('name','')
			code = d.get('default_code',False)
			if code:
				name = '[%s] %s' % (code,name)
			cn_name = d.get('cn_name',False)
			if cn_name:
				name = '%s, %s' % (name,cn_name)
			if d.get('variants'):
				name = name + ' - %s' % (d['variants'],)
			if d.get('part_no_external'):
				name = '%s, %s' % (name,d['part_no_external'])
				
			return (d['id'], name)

		partner_id = context.get('partner_id', False)

		result = []
		for product in self.browse(cr, user, ids, context=context):
			if product.id <= 0 or not hasattr(product,'name'):
				result.append((product.id,''))
				continue
			sellers = filter(lambda x: x.name.id == partner_id, product.seller_ids)
			if sellers:
				for s in sellers:
					mydict = {
							  'id': product.id,
							  'name': s.product_name or product.name,
							  'cn_name': product.cn_name,
							  'default_code': s.product_code or product.default_code,
							  'variants': product.variants,
							  'part_no_external': product.part_no_external
							  }
					result.append(_name_get(mydict))
			else:
				mydict = {
						  'id': product.id,
						  'name': product.name,
						  'cn_name': product.cn_name,
						  'default_code': product.default_code,
						  'variants': product.variants,
						  'part_no_external': product.part_no_external
						  }
				result.append(_name_get(mydict))
		return result	
	#增加part_no_external的搜索
	
	#Add cn_name search
	def _name_search_domain(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
		name_domain = [('default_code',operator,name),('name',operator,name),('ean13',operator,name),\
					('cn_name',operator,name),('cn_name',operator,name),('part_no_external',operator,name)]
		return name_domain
#	
#	def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
#		if not args:
#			args = []
#		if name:
#			#get domain by name
#			name_domain = self._name_search_domain(cr, user, name, args, operator, context, limit)
#			#add '|' operator
#			field_cnt = len(name_domain) 
#			for i in range(1, field_cnt):
#				idx = field_cnt-i-1
#				name_domain.insert(idx, '|')
#			domain_search = name_domain + args
#			if name_domain:
#				ids = self.search(cr, user, domain_search, limit=limit, context=context)
#			if not ids:
#				ptrn = re.compile('(\[(.*?)\])')
#				res = ptrn.search(name)
#				if res:
#					ids = self.search(cr, user, [('default_code','=', res.group(2))] + args, limit=limit, context=context)
#		else:
#			ids = self.search(cr, user, args, limit=limit, context=context)
#		result = self.name_get(cr, user, ids, context=context)
#		return result
		
product_product()
